package cn.cloudtogo.server.mongo.service;

import cn.cloudtogo.common.types.RequestHeaderObject;
import cn.cloudtogo.server.mongo.SpringMongoTemplateContext;
import cn.cloudtogo.support.mongo.collection.CollectionApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static cn.cloudtogo.common.util.StringHelper.pretty;

/**
 * @author yaolianhua789@gmail.com
 **/
@Component
@Slf4j
public class Collections implements CollectionApi {

    private final SpringMongoTemplateContext mongoTemplateContext;

    public Collections(SpringMongoTemplateContext mongoTemplateContext) {
        this.mongoTemplateContext = mongoTemplateContext;
    }

    @Override
    public List<String> collections(RequestHeaderObject object) {
        log.debug(pretty(object, "List collections name", null, null));
        MongoTemplate mongoTemplate = mongoTemplateContext.getMongoTemplate(object);
        Set<String> collectionNames = mongoTemplate.getCollectionNames();

        return new LinkedList<>(collectionNames);
    }
}
